﻿/*
 *  SvnCommitToFTP is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 
 *  SvnCommitToFTP is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with SvnCommitToFTP.  If not, see <http://www.gnu.org/licenses/>.
 *  
 *  Copyright 2008 Alexandru Petrescu (alex.petrescu@gmail.com)
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Softec.AprSharp;
using Softec.SubversionSharp;

namespace SvnCommitToFTP
{
    class Program
    {
       
        static void Main(string[] args)
        {
            string svnPath;
            string svnRev;
            System.IO.TextWriter sw = null;

            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ConformanceLevel = ConformanceLevel.Fragment;
            settings.IgnoreWhitespace = true;
            settings.IgnoreComments = true;

            string settingsPath = "settings.xml";

            if(args.Length == 2)
            {
                settingsPath = args[0] + @"\hooks\" + settingsPath;
            }
            XmlReader xr = XmlReader.Create(settingsPath, settings);

            XmlDocument xd = new XmlDocument();
            xd.Load(xr);

            if (args.Length == 2)
            {
                svnPath = args[0];
                svnRev = args[1];
                if(svnPath[1] == ':') // Repo Path is sometimes sent with the C:/, so remove that, because it's not needed.
                {
                svnPath = svnPath.Substring(2);
                }
                sw = new System.IO.StreamWriter(xd["Settings"]["Logging"]["Path"].InnerText, true, System.Text.Encoding.Default);
             
             
            }
            else
            { // FOR DEBUGGING
                svnPath = "file:///SVN/TestProj";
                svnRev = "10";
                sw = Console.Out;
            }

            
            sw.WriteLine(System.DateTime.Now.ToString() + ": Starting." + string.Join("|", args));
            sw.WriteLine("Path: "+svnPath+" - Rev: "+svnRev);
            sw.WriteLine("");

            try
            {
                SvnCommitToFTP obj = new SvnCommitToFTP(svnPath, svnRev, sw);

                obj.ftpServerURL = xd["Settings"]["FTP"]["Host"].InnerText;
                obj.ftpUserID = xd["Settings"]["FTP"]["User"].InnerText;
                obj.ftpPassword = xd["Settings"]["FTP"]["Password"].InnerText;
                obj.ftpRemoteDir = xd["Settings"]["FTP"]["RemotePath"].InnerText;

                obj.execute();
            }
            catch(Exception e)
            {
                sw.WriteLine(e.Message);
            }

            sw.WriteLine(System.DateTime.Now.ToString() + ": Done");
            sw.WriteLine("");
            if(sw != null)
                sw.Close();

        }
    }
}
